package com.wcx.application.controller;

import com.wcx.application.domian.AdminBean;
import com.wcx.application.domian.ResumeBean;
import com.wcx.application.domian.StudentBean;
import com.wcx.application.service.AdminService;
import com.wcx.application.service.ResumeService;
import com.wcx.application.service.StudentService;
import com.wcx.application.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

@RestController
@RequestMapping("/student")
@CrossOrigin
public class StudentController {

    @Autowired
    StudentService studentService;
    @Autowired
    ResumeController resumeController;
    @Value("${web.upload-path}")
    private String uploadPath;

//1
    @GetMapping("/list")
    public Result getList() {
        System.out.println("---" + studentService.getList());
        return Result.ok().data("list", studentService.getList());
    }

    @PostMapping("/update")
    public Result update(@RequestBody StudentBean studentBean) {
        int i = studentService.update(studentBean);
        if (i != 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @PostMapping("/login")
    public Result login(@RequestBody StudentBean studentBean) {
        StudentBean login = studentService.login(studentBean);

        if (login != null) {
            return Result.ok().data("data", login);
        } else {
            return Result.error();
        }
    }

    @PostMapping("/search")
    public Result search(@RequestBody StudentBean studentBean) {
        return Result.ok().data("list", studentService.searchList(studentBean));
    }

    @PostMapping("/create")
    public Result create(@RequestBody StudentBean studentBean) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY年MM月dd日 HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        String time = simpleDateFormat.format(calendar.getTime());

        studentBean.setCreateTime(time);
        if (studentBean.getAvatar() == "" || studentBean.getAvatar() == null) {
            studentBean.setAvatar("917488dc_E380397_8f92a53e.png");
        }
        int i = studentService.create(studentBean);
        int id = studentService.search(studentBean.getUsername());
        ResumeBean resumeBean = new ResumeBean();
        resumeBean.setStudentId(id);
        System.out.println(resumeBean.getStudentId()+"..."+studentBean.getId());
        resumeController.create(resumeBean);
        if (i != 0) {
            return Result.ok();
        } else {
            return Result.error();
        }

    }

    @DeleteMapping("/delete")
    public Result delete(int id) {
        int i = studentService.delete(id);
        if (i != 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile photo, @RequestParam("id") int id, HttpServletRequest request) {
        System.out.println(photo.getOriginalFilename());
        System.out.println(photo.getContentType());

        int i = studentService.updateAvatarById(photo.getOriginalFilename(), id);
        saveFile(photo, uploadPath);
        if (i != 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @PostMapping("/uploadA")
    public Result uploadA(@RequestParam("file") MultipartFile photo, String username, HttpServletRequest request) {
        System.out.println(photo.getOriginalFilename());
        System.out.println(photo.getContentType());

        int i = studentService.updateAvatarByUserName(photo.getOriginalFilename(), username);
        saveFile(photo, uploadPath);
        if (i != 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    private void saveFile(MultipartFile photo, String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
        File file1 = new File(path + photo.getOriginalFilename());
        try {
            photo.transferTo(file1);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}